<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link href="public/css.css" rel="stylesheet" type="text/css" />
        <title>web2</title>
    </head>

    <body>
        <div class="chung">
            <?php
            require_once './model/works.php';
            $keyword = new Works();
            $keyword->table_name = "works";
            ?>
            <?php
            if (!isset($_GET["page"]))
                $page = 1;
            else
                $page = $_GET["page"];
            $total = $keyword->countWords();
            $wor = $keyword->noidung(null, $_GET['id']);
            ?>
            <div class="hinh"></div>
            <h2>C12TDT</h2>
            <div class="menu">
                <ul>
                    <li><a href="index.php"> Menu</a></li>
                    <li><a href="#"> Content types</a></li>
                    <li><a href="#"> Forums</a></li>
                    <li><a href="#"> Contact</a></li>
                    <li> <input type="text" id="type"/><input type="button" value="go" id="ty"></li>
                </ul>
                <div class="thanh"></div>

            </div>

            <div class="noidung">
                <hr id="like"></hr>

                <div class="nav" style="clear: both;"> 
                    <ul>
                        <?php if (!isset($_GET["page"])):
                            ?>

                            <a href="index.php?page=<?php echo $_GET['page'] = 1 ?>">
                            </a>
                        <?php endif; ?>


                        <li> <a href="index.php?page=1">Fist</a></li>
                        <li><a href="index.php?page=<?php echo $_GET["page"] - 1 ?>"><< Prev</a></li>

                        <?php for ($i = 1; $i <= 7; $i++): ?>
                            <?php if (($_GET["page"] == 1) || ($_GET["page"] == 2) || ($_GET["page"] == 3) || ($_GET["page"] == 4)): ?>            
                                <?php for ($i = 1; $i <= 7; $i++): ?>
                                    <li><a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a></li>
                                    <?php if ($_GET["page"] == $i): ?>
                                        <?php $keyword->begin; ?>
                                        <?php $keyword->theend = PAGE; ?>
                                    <?php endif; ?>
                                <?php endfor; ?>
                            <?php elseif (($_GET["page"] == $total / PAGE) || ($_GET["page"] == $total / PAGE - 1) || ($_GET["page"] == $total / PAGE - 2) || ($_GET["page"] == $total / PAGE - 3)): ?>        
                                <?php for ($i = $total / PAGE - 7; $i <= $total / PAGE; $i++): ?>
                                    <li><a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a></li>
                                    <?php if ($_GET["page"] == $i): ?>
                                        <?php $keyword->begin; ?>
                                        <?php $keyword->theend = PAGE; ?>
                                    <?php endif; ?>
                                <?php endfor; ?>
                            <?php else : ?>
                                <?php for ($i = $_GET["page"] - 3; $i <= $_GET["page"] + 3; $i++): ?>
                                    <li><a href='index.php?page=<?php echo $i; ?> '><?php echo $i ?></a></li>
                                    <?php if ($_GET["page"] == $i): ?>
                                        <?php $keyword->begin; ?>
                                        <?php $keyword->theend = PAGE; ?>
                                    <?php endif; ?>
                                <?php endfor; ?>
                            <?php endif; ?> 
                        <?php endfor; ?>
                        <li><a href="index.php?page=<?php echo $_GET['page'] + 1 ?>">Next >></a></li>           
                        <li><a href="index.php?page=<?php echo $total / PAGE ?>">Last</a></li>

                        <?php $keywords = $keyword->select($page); ?>
                    </ul>
                </div>

                <table border="1px" style="clear: both;" class="nd">
                    <form method="post"      enctype="multipart/form-data">
                        <label for="file">Namework :</label>
                        <input type="text" name="name" class="namea" value="<?php 
                                               if(!isset($_POST["submit"]))
                                                    echo $wor['name']; 
                                               else
                                                   echo $_POST["name"];
                                               ?>"><br>
                        <label for="file">Locationwork :</label>
                        <input type="text" name="location" class="nameb" value="<?php 
                                            if(!isset($_POST["submit"]))
                                                    echo $wor['location']; 
                                               else
                                           echo $_POST['location']; 
                                           ?>"><br>
                        <label for="file">Filename:</label>
                        <input type="file" name="file" id="file"><br>
                        <input type="submit" name="submit" value="Submit"><br>
                    </form>
                          <?php
                            if (isset($_POST['submit'])) {
                                $allowedExts = array("gif", "jpeg", "jpg", "png");
                                $temp = explode(".", $_FILES["file"]["name"]);  //ten hinh va duoi file
                                $extension = end($temp); //hien cai duoi
                                if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && in_array($extension, $allowedExts)) {
                                    if ($_FILES["file"]["error"] > 0) {
                                        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
                                    } else {
                                     echo "Upload: " . $_FILES["file"]["name"] . "<br>";
                                        echo "Type: " . $_FILES["file"]["type"] . "<br>";
                                       echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
                                       echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
                                        $file_name = $temp[0] . '-' . time() . '.' . $temp[1]; //upload cung luc ko loi
                                        if (file_exists("public/images/" . $_FILES["file"]["name"])) {
                                            echo $_FILES["file"]["name"] . " already exists. ";
                                        } else {
                                            move_uploaded_file($_FILES["file"]["tmp_name"], "public/images/" . $file_name);
                                         echo "Stored in: " . "public/images/" . $file_name . "</br>";
                                        }
                                        $array = array(
                                            "id" => $_GET["id"],
                                            "name" => $_POST["name"],
                                            "location" => $_POST["location"],
                                            "image" => "public/images/" . $file_name
                                        );
                                        $keyword->update($array);
                                        $url = "public/images/" . $file_name;
                                        echo "<img src='$url ' width='150px'>";
                                    }
                                } else {
                                    $array = array(
                                            "id" => $_GET["id"],
                                            "name" => $_POST["name"],
                                            "location" => $_POST["location"]);
                                    $keyword->update_nohinh($array);
                                }
                                //echo "<img src='$url ' width='150px'>";
                            }
                            ?>
                </table>

                        <img src="public/images/anhdong1.gif">
                        <img src="public/images/anhdong2.gif">
            </div>
            <div class="cuoi">Huỳnh Thị Bích Vân C12TDT</div>

        </div>
    </body>
</html>
